home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_051 / bison / reader.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  31KB  |  1,578 lines

  1. /* Input parser for bison, copyright (C) 1984 Bob Corbett and Richard Stallman
  2.  
  3.    Permission is granted to anyone to make or distribute verbatim copies of this program
  4.    provided that the copyright notice and this permission notice are preserved;
  5.    and provided that the recipient is not asked to waive or limit his right to
  6.    redistribute copies as permitted by this permission notice;
  7.    and provided that anyone possessing an executable copy
  8.    is granted access to copy the source code, in machine-readable form,
  9.    in some reasonable manner.
  10.  
  11.    Permission is granted to distribute derived works or enhanced versions of
  12.    this program under the above conditions with the additional condition
  13.    that the entire derivative or enhanced work
  14.    must be covered by a permission notice identical to this one.
  15.  
  16.    Anything distributed as part of a package containing portions derived
  17.    from this program, which cannot in current practice perform its function usefully
  18.    in the absense of what was derived directly from this program,
  19.    is to be considered as forming, together with the latter,
  20.    a single work derived from this program,
  21.    which must be entirely covered by a permission notice identical to this one
  22.    in order for distribution of the package to be permitted.
  23.  
  24.  In other words, you are welcome to use, share and improve this program.
  25.  You are forbidden to forbid anyone else to use, share and improve
  26.  what you give them.   Help stamp out software-hoarding!  */
  27.  
  28. /* read in the grammar specification and record it in the format described in gram.h.
  29.   All guards are copied into the fguard file and all actions into faction,
  30.   in each case forming the body of a C function (yyguard or yyaction)
  31.   which contains a switch statement to decide which guard or action to execute.
  32.  
  33. The entry point is reader().  */
  34.  
  35. #include <stdio.h>
  36. #include <ctype.h>
  37. #include "files.h"
  38. #include "new.h"
  39. #include "symtab.h"
  40. #include "lex.h"
  41. #include "gram.h"
  42.  
  43.  
  44. #define    LTYPESTR    "\n#ifndef YYLTYPE\ntypedef\n  struct yyltype\n\
  45.     {\n      int timestamp;\n      int first_line;\n      int first_column;\n\
  46.       int last_line;\n      int last_column;\n      char *text;\n   }\n\
  47.   yyltype;\n\n#define YYLTYPE yyltype\n#endif\n\n"
  48.  
  49.  
  50. extern int definesflag;
  51. extern bucket *symval;
  52. extern int numval;
  53.  
  54. typedef
  55.   struct symbol_list
  56.     {
  57.       struct symbol_list *next;
  58.       bucket *sym;
  59.       bucket *ruleprec;
  60.     }
  61.   symbol_list;
  62.  
  63.  
  64.  
  65. int lineno;
  66. bucket *symval;
  67. symbol_list *grammar;
  68. int start_flag;
  69. bucket *startval;
  70. char **tags;
  71.  
  72. static int typed;  /* nonzero if %union has been seen.  */
  73.  
  74. static int lastprec;  /* incremented for each %left, %right or %nonassoc seen */
  75.  
  76. static int gensym_count;  /* incremented for each generated symbol */
  77.  
  78. static bucket *errtoken;
  79.  
  80. static FILE *fattrs1;
  81.  
  82. reader()
  83. {
  84.  
  85.   start_flag = 0;
  86.   startval = NULL;  /* start symbol not specified yet. */
  87.  
  88.   translations = 0;  /* initially assume token number translation not needed.  */
  89.  
  90.   nsyms = 1;
  91.   nvars = 0;
  92.   nrules = 0;
  93.   nitems = 0;
  94.  
  95.   typed = 0;
  96.   lastprec = 0;
  97.  
  98.   gensym_count = 0;
  99.  
  100.   semantic_parser = 0;
  101.   pure_parser = 0;
  102.  
  103.   grammar = NULL;
  104.  
  105.   fattrs1 = ftable;   /* Unless/until fattrs is opened, use ftable instead.  */
  106.  
  107.   init_lex();
  108.   lineno = 1;
  109.  
  110.   /* initialize the symbol table.  */
  111.   tabinit();
  112.   /* construct the error token */
  113.   errtoken = getsym("error");
  114.   errtoken->class = STOKEN;
  115.   /* construct a token that represents all undefined literal tokens. */
  116.   /* it is always token number 2.  */
  117.   getsym("$illegal.")->class = STOKEN;
  118.   /* Read the declaration section.  Copy %{ ... %} groups to ftable or fattrs file.
  119.      Also notice any %token, %left, etc. found there.  */
  120.   fprintf(ftable, "\n/*  A Bison parser, made from %s  */\n\n", infile);
  121.   read_declarations();
  122.   /* output the definition of YYLTYPE into the fattrs or ftable file.  */
  123.   output_ltype();
  124.   /* start writing the guard and action files, if they are needed.  */
  125.   output_headers();
  126.   /* read in the grammar, build grammar in list form.  write out guards and actions.  */
  127.   readgram();
  128.   /* write closing delimiters for actions and guards.  */
  129.   output_trailers();
  130.   /* assign the symbols their symbol numbers.
  131.      Write #defines for the token symbols into fdefines if requested.  */
  132.   packsymbols();
  133.   /* convert the grammar into the format described in gram.h.  */
  134.   packgram();
  135.   /* free the symbol table data structure
  136.      since symbols are now all referred to by symbol number.  */
  137.   free_symtab();
  138. }
  139.  
  140.  
  141.  
  142. /* read from finput until %% is seen.  Discard the %%.
  143. Handle any % declarations,
  144. and copy the contents of any %{ ... %} groups to ftable or fattrs.  */
  145.  
  146. read_declarations ()
  147. {
  148.   register int c;
  149.   register int tok;
  150.  
  151.   for (;;)
  152.     {
  153.       c = skip_white_space();
  154.  
  155.       if (c == '%')
  156.     {
  157.       tok = parse_percent_token();
  158.  
  159.       switch (tok)
  160.         {
  161.         case TWO_PERCENTS:
  162.           return;
  163.  
  164.         case PERCENT_LEFT_CURLY:
  165.           copy_definition();
  166.           break;
  167.  
  168.         case TOKEN:
  169.           parse_token_decl (STOKEN, SNTERM);
  170.           break;
  171.     
  172.         case NTERM:
  173.           parse_token_decl (SNTERM, STOKEN);
  174.           break;
  175.     
  176.         case TYPE:
  177.           parse_type_decl();
  178.           break;
  179.     
  180.         case START:
  181.           parse_start_decl();
  182.           break;
  183.     
  184.         case UNION:
  185.           parse_union_decl();
  186.           break;
  187.     
  188.         case LEFT:
  189.           parse_assoc_decl(LEFT_ASSOC);
  190.           break;
  191.  
  192.         case RIGHT:
  193.           parse_assoc_decl(RIGHT_ASSOC);
  194.           break;
  195.  
  196.         case NONASSOC:
  197.           parse_assoc_decl(NON_ASSOC);
  198.           break;
  199.  
  200.         case SEMANTIC_PARSER:
  201.           semantic_parser = 1;
  202.           open_extra_files();
  203.           fattrs1 = fattrs;
  204.           break;
  205.  
  206.         case PURE_PARSER:
  207.           pure_parser = 1;
  208.           break;
  209.  
  210.         default:
  211.           fatal("junk after % in definition section");
  212.         }
  213.     }
  214.       else if (c == EOF)
  215.         fatal("no input grammar");
  216.       else
  217.         fatal("junk in declaration section");
  218.  
  219.     }
  220. }
  221.  
  222.  
  223. /* copy the contents of a %{ ... %} into the definitions file.
  224. The %{ has already been read.  Return after reading the %}.  */
  225. copy_definition ()
  226. {
  227.   register int c;
  228.   register int match;
  229.   register int ended;
  230.   register int after_percent;  /* -1 while reading a character if prev char was % */
  231.  
  232.   fprintf(fattrs1, "#line %d \"%s\"\n", lineno, infile);
  233.  
  234.   after_percent = 0;
  235.  
  236.   c = getc(finput);
  237.  
  238.   for (;;)
  239.     {
  240.       switch (c)
  241.     {
  242.     case '\n':
  243.       putc(c, fattrs1);
  244.       lineno++;
  245.       break;
  246.  
  247.     case '%':
  248.           after_percent = -1;
  249.       break;
  250.           
  251.     case '\'':
  252.     case '"':
  253.       match = c;
  254.       putc(c, fattrs1);
  255.       c = getc(finput);
  256.  
  257.       while (c != match)
  258.         {
  259.           if (c == EOF || c == '\n')
  260.         fatal("unterminated string");
  261.  
  262.           putc(c, fattrs1);
  263.           
  264.           if (c == '\\')
  265.         {
  266.           c = getc(finput);
  267.           if (c == EOF || c == '\n')
  268.             fatal("unterminated string");
  269.           putc(c, fattrs1);
  270.           if (c == '\n')
  271.             lineno++;
  272.         }
  273.  
  274.           c = getc(finput);
  275.         }
  276.  
  277.       putc(c, fattrs1);
  278.       break;
  279.  
  280.     case '/':
  281.       putc(c, fattrs1);
  282.       c = getc(finput);
  283.       if (c != '*')
  284.         continue;
  285.  
  286.       putc(c, fattrs1);
  287.       c = getc(finput);
  288.  
  289.       ended = 0;
  290.       while (!ended)
  291.         {
  292.           if (c == '*')
  293.         {
  294.           while (c == '*')
  295.             {
  296.               putc(c, fattrs1);
  297.               c = getc(finput);
  298.             }
  299.  
  300.           if (c == '/')
  301.             {
  302.               putc(c, fattrs1);
  303.               ended = 1;
  304.             }
  305.         }
  306.           else if (c == '\n')
  307.         {
  308.           lineno++;
  309.           putc(c, fattrs1);
  310.           c = getc(finput);
  311.         }
  312.           else if (c == EOF)
  313.         fatal("unterminated comment in %{ definition");
  314.           else
  315.         {
  316.           putc(c, fattrs1);
  317.           c = getc(finput);
  318.         }
  319.         }
  320.  
  321.       break;
  322.  
  323.     case EOF:
  324.       fatal("unterminated %{ definition");
  325.  
  326.     default:
  327.       putc(c, fattrs1);
  328.     }
  329.  
  330.       c = getc(finput);
  331.  
  332.       if (after_percent)
  333.     {
  334.       if (c == '}')
  335.         return;
  336.       putc('%', fattrs1);
  337.     }
  338.       after_percent = 0;
  339.  
  340.     }
  341.  
  342. }
  343.  
  344.  
  345.  
  346. /* parse what comes after %token or %nterm.
  347. For %token, what_is is STOKEN and what_is_not is SNTERM.
  348. For %nterm, the arguments are reversed.  */
  349.  
  350. parse_token_decl (what_is, what_is_not)
  351. int what_is, what_is_not;
  352. {
  353.   register int start_lineno;
  354.   register int token;
  355.   register int prev = 0;
  356.   register int readtoken;
  357.   int k;
  358.   register char *name = NULL;
  359.  
  360.   extern char token_buffer[];
  361.  
  362.   if ((token = lex()) == TYPENAME) {
  363.       k = strlen(token_buffer);
  364.       name = NEW2(k + 1, char);
  365.       strcpy(name, token_buffer);
  366.   }
  367.   readtoken = (name != NULL);
  368.  
  369.   start_lineno = lineno;
  370.  
  371.   for (;;)
  372.     {
  373.       if(readtoken) {
  374.       ungetc(skip_white_space(), finput);
  375.  
  376.       if (lineno != start_lineno)
  377.           return;
  378.  
  379.           /* we have not passed a newline, so the token now starting
  380.          is in this declaration */
  381.       prev = token;
  382.       token = lex();
  383.       }
  384.       readtoken = 1;
  385.  
  386.       if (token == IDENTIFIER)
  387.     {
  388.       if (symval->class == what_is_not)
  389.         fatals("symbol %s redefined", symval->tag);
  390.       symval->class = what_is;
  391.       if (what_is == SNTERM)
  392.         symval->value = nvars++;
  393.       if(name)
  394.         symval->type_name = name;
  395.     }
  396.       else if (prev == IDENTIFIER && token == NUMBER)
  397.         {
  398.       symval->user_token_number = numval;
  399.       translations = 1;
  400.         }
  401.       else      
  402.     fatal("illegal text in %token or %nterm declaration");
  403.     }
  404.  
  405. }
  406.  
  407.  
  408.  
  409. /* parse what comes after %start */
  410.  
  411. parse_start_decl ()
  412. {
  413.   if (start_flag)
  414.     fatal("multiple start declarations");
  415.   start_flag = 1;
  416.   if (lex() != IDENTIFIER)
  417.     fatal("illegal start declaration");
  418.   startval = symval;
  419. }
  420.  
  421.  
  422.  
  423. /* read in a %type declaration and record its information for get_type_name to access */
  424.  
  425. parse_type_decl ()
  426. {
  427.   register int k;
  428.   register char *name;
  429.   register int start_lineno;
  430.  
  431.   extern char token_buffer[];
  432.  
  433.   if (lex() != TYPENAME)
  434.     fatal("ill-formed %type declaration");
  435.  
  436.   k = strlen(token_buffer);
  437.   name = NEW2(k + 1, char);
  438.   strcpy(name, token_buffer);
  439.  
  440.   start_lineno = lineno;
  441.  
  442.   for (;;)
  443.     {
  444.       register int t;
  445.  
  446.       ungetc(skip_white_space(), finput);
  447.  
  448.       if (lineno != start_lineno)
  449.     return;
  450.  
  451.       /* we have not passed a newline, so the token now starting is in this declaration */
  452.  
  453.       t = lex();
  454.  
  455.       switch (t)
  456.     {
  457.  
  458.     case COMMA:
  459.       break;
  460.  
  461.     case IDENTIFIER:
  462.       if (symval->type_name == NULL)
  463.         symval->type_name = name;
  464.       else
  465.         fatals("type redeclaration for %s", symval->tag);
  466.  
  467.       break;
  468.  
  469.     default:
  470.       fatal("illegal %type declaration");
  471.     }
  472.     }
  473. }
  474.  
  475.  
  476.  
  477. /* read in a %left, %right or %nonassoc declaration and record its information.  */
  478. /* assoc is either LEFTASSOC, RIGHTASSOC or NONASSOC.  */
  479.  
  480. parse_assoc_decl (assoc)
  481. int assoc;
  482. {
  483.   register int k;
  484.   register char *name = NULL;
  485.   register int start_lineno;
  486.   register int prev;
  487.  
  488.   extern char token_buffer[];
  489.  
  490.   lastprec++;  /* assign a new precedence level.  */
  491.  
  492.   start_lineno = lineno;
  493.  
  494.   for (;;)
  495.     {
  496.       register int t;
  497.  
  498.       ungetc(skip_white_space(), finput);
  499.  
  500.       if (lineno != start_lineno)
  501.     return;
  502.  
  503.       /* we have not passed a newline, so the token now starting is in this declaration */
  504.  
  505.       t = lex();
  506.  
  507.       switch (t)
  508.     {
  509.  
  510.     case TYPENAME:
  511.       k = strlen(token_buffer);
  512.       name = NEW2(k + 1, char);
  513.       strcpy(name, token_buffer);
  514.       break;
  515.  
  516.     case COMMA:
  517.       break;
  518.  
  519.     case IDENTIFIER:
  520.       symval->prec = lastprec;
  521.       symval->assoc = assoc;
  522.       if (symval->class == SNTERM)
  523.         fatals("symbol %s redefined", symval->tag);
  524.       symval->class = STOKEN;
  525.       if (name)
  526.         { /* record the type, if one is specified */
  527.           if (symval->type_name == NULL)
  528.         symval->type_name = name;
  529.           else
  530.         fatals("type redeclaration for %s", symval->tag);
  531.         }
  532.       break;
  533.  
  534.     case NUMBER:
  535.       if (prev == IDENTIFIER)
  536.             {
  537.           symval->user_token_number = numval;
  538.           translations = 1;
  539.             }
  540.           else      
  541.         fatal("illegal text in association declaration");
  542.       break;
  543.  
  544.     case SEMICOLON:
  545.       return;
  546.  
  547.     default:
  548.       fatal("malformatted association declaration");
  549.     }
  550.  
  551.       prev = t;
  552.  
  553.     }
  554. }
  555.  
  556.  
  557.  
  558. /* copy the union declaration into ftable or fattrs, where it is made into the
  559. definition of YYSTYPE, the type of elements of the parser value stack.  */
  560.  
  561. parse_union_decl()
  562. {
  563.   register int c;
  564.   register int count;
  565.   register int in_comment;
  566.  
  567.   if (typed)
  568.     fatal("multiple %union declarations");
  569.  
  570.   typed = 1;
  571.  
  572.   fprintf(fattrs1, "\n#line %d \"%s\"\n", lineno, infile);
  573.   fprintf(fattrs1, "typedef union");
  574.  
  575.   count = 0;
  576.   in_comment = 0;
  577.  
  578.   c = getc(finput);
  579.  
  580.   while (c != EOF)
  581.     {
  582.       putc(c, fattrs1);
  583.  
  584.       switch (c)
  585.     {
  586.     case '\n':
  587.       lineno++;
  588.       break;
  589.  
  590.     case '/':
  591.       c = getc(finput);
  592.       if (c != '*')
  593.         ungetc(c, finput);
  594.       else
  595.         {
  596.           putc('*', fattrs1);
  597.           c = getc(finput);
  598.           in_comment = 1;
  599.           while (in_comment)
  600.         {
  601.           if (c == EOF)
  602.             fatal("unterminated comment");
  603.  
  604.           putc(c, fattrs1);
  605.           if (c == '*')
  606.             {
  607.               c = getc(finput);
  608.               if (c == '/')
  609.             {
  610.               putc('/', fattrs1);
  611.               in_comment = 0;
  612.             }
  613.             }
  614.           else
  615.             c = getc(finput);
  616.         }
  617.         }
  618.       break;
  619.  
  620.  
  621.     case '{':
  622.       count++;
  623.       break;
  624.  
  625.     case '}':
  626.       count--;
  627.       if (count == 0)
  628.         {
  629.           fprintf(fattrs1, " YYSTYPE;\n");
  630.           return;
  631.         }
  632.     }
  633.  
  634.       c = getc(finput);
  635.     }
  636. }
  637.  
  638. /* that's all of parsing the declaration section */
  639.  
  640. output_ltype()
  641. {
  642.   fprintf(fattrs1, LTYPESTR);
  643.   fprintf(fattrs1, "#define\tYYACCEPT\treturn(0)\n#define\tYYERROR\treturn(1)\n");
  644. }
  645.  
  646.  
  647.  
  648. /* Get the data type (alternative in the union) of the value for symbol n in rule rule.  */
  649.  
  650. char *
  651. get_type_name(n, rule)
  652. int n;
  653. symbol_list *rule;
  654. {
  655.   static char *msg = "illegal $ value";
  656.  
  657.   register int i;
  658.   register symbol_list *rp;
  659.  
  660.   if (n < 0)
  661.     fatal(msg);
  662.  
  663.   rp = rule;
  664.   i = 0;
  665.  
  666.   while (i < n)
  667.     {
  668.       rp = rp->next;
  669.       if (rp == NULL || rp->sym == NULL)
  670.     fatal(msg);
  671.       i++;
  672.     }
  673.  
  674.   return (rp->sym->type_name);
  675. }
  676.  
  677.  
  678.  
  679. /* after %guard is seen in the input file,
  680. copy the actual guard into the guards file.
  681. If the guard is followed by an action, copy that into the actions file.
  682. stack_offset is the number of values in the current rule so far,
  683. which says where to find $0 with respect to the top of the stack,
  684. for the simple parser in which the stack is not popped until after the guard is run.  */
  685.  
  686. copy_guard(rule, stack_offset)
  687. symbol_list *rule;
  688. int stack_offset;
  689. {
  690.   register int c;
  691.   register int n;
  692.   register int count;
  693.   register int match;
  694.   register int ended;
  695.   register char *type_name;
  696.  
  697.   /* offset is always 0 if parser has already popped the stack pointer */
  698.   if (semantic_parser) stack_offset = 0;
  699.  
  700.   fprintf(fguard, "\ncase %d:\n", nrules);
  701.   fprintf(fguard, "#line %d \"%s\"\n", lineno, infile);
  702.   putc('{', fguard);
  703.  
  704.   count = 0;
  705.   c = getc(finput);
  706.  
  707.   while (count > 0 || c != ';' && c != '{')
  708.     {
  709.       switch (c)
  710.     {
  711.     case '\n':
  712.       putc(c, fguard);
  713.       lineno++;
  714.       break;
  715.  
  716.     case '{':
  717.       putc(c, fguard);
  718.       count++;
  719.       break;
  720.  
  721.     case '}':
  722.       putc(c, fguard);
  723.       if (count > 0)
  724.         count--;
  725.       else
  726.         fatal("unmatched right brace ('}')");
  727.  
  728.     case '\'':
  729.     case '"':
  730.       match = c;
  731.       putc(c, fguard);
  732.       c = getc(finput);
  733.  
  734.       while (c != match)
  735.         {
  736.           if (c == EOF || c == '\n')
  737.         fatal("unterminated string");
  738.  
  739.           putc(c, fguard);
  740.           
  741.           if (c == '\\')
  742.         {
  743.           c = getc(finput);
  744.           if (c == EOF || c == '\n')
  745.             fatal("unterminated string");
  746.           putc(c, fguard);
  747.           if (c == '\n')
  748.             lineno++;
  749.         }
  750.  
  751.           c = getc(finput);
  752.         }
  753.  
  754.       putc(c, fguard);
  755.       break;
  756.  
  757.     case '/':
  758.       putc(c, fguard);
  759.       c = getc(finput);
  760.       if (c != '*')
  761.         continue;
  762.  
  763.       putc(c, fguard);
  764.       c = getc(finput);
  765.  
  766.       ended = 0;
  767.       while (!ended)
  768.         {
  769.           if (c == '*')
  770.         {
  771.           while (c == '*')
  772.             {
  773.               putc(c, fguard);
  774.               c = getc(finput);
  775.             }
  776.  
  777.           if (c == '/')
  778.             {
  779.               putc(c, fguard);
  780.               ended = 1;
  781.             }
  782.         }
  783.           else if (c == '\n')
  784.         {
  785.           lineno++;
  786.           putc(c, fguard);
  787.           c = getc(finput);
  788.         }
  789.           else if (c == EOF)
  790.         fatal("unterminated comment");
  791.           else
  792.         {
  793.           putc(c, fguard);
  794.           c = getc(finput);
  795.         }
  796.         }
  797.  
  798.       break;
  799.  
  800.     case '$':
  801.       c = getc(finput);
  802.       type_name = NULL;
  803.  
  804.       if (c == '<')
  805.         {
  806.           register char *cp = token_buffer;
  807.  
  808.           while ((c = getc(finput)) != '>' && c > 0)
  809.         *cp++ = c;
  810.           *cp = 0;
  811.           type_name = token_buffer;
  812.  
  813.           c = getc(finput);
  814.         }
  815.  
  816.       if (c == '$')
  817.         {
  818.           fprintf(fguard, "yyval");
  819.           if (!type_name) type_name = rule->sym->type_name;
  820.           if (type_name)
  821.         fprintf(fguard, ".%s", type_name);
  822.         }
  823.  
  824.       else if (isdigit(c) || c == '-')
  825.         {
  826.           register int sign = 1;
  827.  
  828.           if (c == '-')
  829.         {
  830.           c = getc(finput);
  831.           sign = -1;
  832.         }
  833.           n = 0;
  834.           while (isdigit(c))
  835.         {
  836.           n = 10*n + (c - '0');
  837.           c = getc(finput);
  838.         }
  839.  
  840.           n *= sign;
  841.  
  842.           if (!type_name && n > 0)
  843.         type_name = get_type_name(n, rule);
  844.  
  845.           fprintf(fguard, "yyvsp[%d]", n - stack_offset);
  846.           if (type_name)
  847.         fprintf(fguard, ".%s", type_name);
  848.           continue;
  849.         }
  850.       else
  851.         fatal ("illegal $-construct");
  852.  
  853.       break;
  854.  
  855.     case '@':
  856.       c = getc(finput);
  857.       n = 0;
  858.       while (isdigit(c))
  859.         {
  860.           n = 10*n + (c - '0');
  861.           c = getc(finput);
  862.         }
  863.  
  864.       if (n < 1)
  865.         fatal("illegal @-construct");
  866.  
  867.       fprintf(fguard, "yylsp[%d]", n - stack_offset);
  868.  
  869.       continue;
  870.  
  871.     case EOF:
  872.       fatal("unterminated %guard clause");
  873.  
  874.     default:
  875.       putc(c, fguard);
  876.     }
  877.  
  878.       c = getc(finput);
  879.     }
  880.  
  881.   fprintf(fguard, ";\n    break;}");
  882.   if (c == '{')
  883.     copy_action(rule, stack_offset);
  884. }
  885.  
  886.  
  887.  
  888. /* Assuming that a { has just been seen, copy everything up to the matching }
  889. into the actions file.
  890. stack_offset is the number of values in the current rule so far,
  891. which says where to find $0 with respect to the top of the stack.  */
  892.  
  893. copy_action(rule, stack_offset)
  894. symbol_list *rule;
  895. int stack_offset;
  896. {
  897.   register int c;
  898.   register int n;
  899.   register int count;
  900.   register int match;
  901.   register int ended;
  902.   register char *type_name;
  903.  
  904.   /* offset is always 0 if parser has already popped the stack pointer */
  905.   if (semantic_parser) stack_offset = 0;
  906.  
  907.   fprintf(faction, "\ncase %d:\n", nrules);
  908.   fprintf(faction, "#line %d \"%s\"\n", lineno, infile);
  909.   putc('{', faction);
  910.  
  911.   count = 1;
  912.   c = getc(finput);
  913.  
  914.   while (count > 0)
  915.     {
  916.       while (c != '}')
  917.         {
  918.           switch (c)
  919.         {
  920.         case '\n':
  921.           putc(c, faction);
  922.           lineno++;
  923.           break;
  924.  
  925.         case '{':
  926.           putc(c, faction);
  927.           count++;
  928.           break;
  929.  
  930.         case '\'':
  931.         case '"':
  932.           match = c;
  933.           putc(c, faction);
  934.           c = getc(finput);
  935.  
  936.           while (c != match)
  937.         {
  938.           if (c == EOF || c == '\n')
  939.             fatal("unterminated string");
  940.  
  941.           putc(c, faction);
  942.  
  943.           if (c == '\\')
  944.             {
  945.               c = getc(finput);
  946.               if (c == EOF)
  947.             fatal("unterminated string");
  948.               putc(c, faction);
  949.               if (c == '\n')
  950.             lineno++;
  951.             }
  952.  
  953.           c = getc(finput);
  954.         }
  955.  
  956.           putc(c, faction);
  957.           break;
  958.  
  959.         case '/':
  960.           putc(c, faction);
  961.           c = getc(finput);
  962.           if (c != '*')
  963.         continue;
  964.  
  965.           putc(c, faction);
  966.           c = getc(finput);
  967.  
  968.           ended = 0;
  969.           while (!ended)
  970.         {
  971.           if (c == '*')
  972.             {
  973.               while (c == '*')
  974.                 {
  975.               putc(c, faction);
  976.               c = getc(finput);
  977.             }
  978.  
  979.               if (c == '/')
  980.             {
  981.               putc(c, faction);
  982.               ended = 1;
  983.             }
  984.             }
  985.           else if (c == '\n')
  986.             {
  987.               lineno++;
  988.               putc(c, faction);
  989.               c = getc(finput);
  990.             }
  991.           else if (c == EOF)
  992.             fatal("unterminated comment");
  993.           else
  994.             {
  995.               putc(c, faction);
  996.               c = getc(finput);
  997.             }
  998.         }
  999.  
  1000.           break;
  1001.  
  1002.         case '$':
  1003.           c = getc(finput);
  1004.           type_name = NULL;
  1005.  
  1006.           if (c == '<')
  1007.         {
  1008.           register char *cp = token_buffer;
  1009.  
  1010.           while ((c = getc(finput)) != '>' && c > 0)
  1011.             *cp++ = c;
  1012.           *cp = 0;
  1013.           type_name = token_buffer;
  1014.  
  1015.           c = getc(finput);
  1016.         }
  1017.           if (c == '$')
  1018.         {
  1019.           fprintf(faction, "yyval");
  1020.           if (!type_name) type_name = get_type_name(0, rule);
  1021.           if (type_name)
  1022.             fprintf(faction, ".%s", type_name);
  1023.         }
  1024.           else if (isdigit(c) || c == '-')
  1025.         {
  1026.           register int sign = 1;
  1027.  
  1028.           if (c == '-')
  1029.             {
  1030.               c = getc(finput);
  1031.               sign = -1;
  1032.             }
  1033.           n = 0;
  1034.           while (isdigit(c))
  1035.             {
  1036.               n = 10*n + (c - '0');
  1037.               c = getc(finput);
  1038.             }
  1039.           n *= sign;
  1040.           if (!type_name && n > 0)
  1041.             type_name = get_type_name(n, rule);
  1042.  
  1043.           fprintf(faction, "yyvsp[%d]", n - stack_offset);
  1044.           if (type_name)
  1045.             fprintf(faction, ".%s", type_name);
  1046.           continue;
  1047.         }
  1048.           else
  1049.         fatal("illegal $-construct");
  1050.  
  1051.           break;
  1052.  
  1053.         case '@':
  1054.           c = getc(finput);
  1055.           n = 0;
  1056.  
  1057.           while (isdigit(c))
  1058.         {
  1059.           n = 10*n + (c - '0');
  1060.           c = getc(finput);
  1061.         }
  1062.  
  1063.           if (n < 1)
  1064.         fatal("illegal @-construct");
  1065.  
  1066.           fprintf(faction, "yylsp[%d]", n - stack_offset);
  1067.  
  1068.           continue;
  1069.  
  1070.         case EOF:
  1071.           fatal("unmatched '{'");
  1072.  
  1073.         default:
  1074.           putc(c, faction);
  1075.         }
  1076.  
  1077.           c = getc(finput);
  1078.         }
  1079.  
  1080.       /* above loop exits when c is '}' */
  1081.  
  1082.       if (--count)
  1083.         {
  1084.       putc(c, faction);
  1085.       c = getc(finput);
  1086.     }
  1087.     }
  1088.  
  1089.   fprintf(faction, ";\n    break;}");
  1090. }
  1091.  
  1092.  
  1093.  
  1094. /* generate a dummy symbol, a nonterminal,
  1095. whose name cannot conflict with the user's names. */
  1096.  
  1097. bucket *
  1098. gensym()
  1099. {
  1100.   register bucket *sym;
  1101.  
  1102.   extern char token_buffer[];
  1103.   sprintf (token_buffer, "@%d", ++gensym_count);
  1104.   sym = getsym(token_buffer);
  1105.   sym->class = SNTERM;
  1106.   sym->value = nvars++;
  1107.   return (sym);
  1108. }
  1109.  
  1110.  
  1111.  
  1112. /* Parse the input grammar into a one symbol_list structure.
  1113. Each rule is represented by a sequence of symbols: the left hand side
  1114. followed by the contents of the right hand side, followed by a null pointer
  1115. instead of a symbol to terminate the rule.
  1116. The next symbol is the lhs of the following rule.
  1117.  
  1118. All guards and actions are copied out to the appropriate files,
  1119. labelled by the rule number they apply to.  */
  1120.  
  1121. readgram()
  1122. {
  1123.   register int t;
  1124.   register bucket *lhs;
  1125.   register symbol_list *p;
  1126.   register symbol_list *p1;
  1127.  
  1128.   symbol_list *crule;    /* points to first symbol_list of current rule.  */
  1129.             /* its symbol is the lhs of the rule.   */
  1130.   symbol_list *crule1;  /* points to the symbol_list preceding crule.  */
  1131.  
  1132.   p1 = NULL;
  1133.  
  1134.   t = lex();
  1135.  
  1136.   while (t != TWO_PERCENTS && t != ENDFILE)
  1137.     {
  1138.       if (t == IDENTIFIER || t == BAR)
  1139.     {
  1140.       register int actionflag = 0;
  1141.       int rulelength = 0;  /* number of symbols in rhs of this rule so far  */
  1142.  
  1143.       if (t == IDENTIFIER)
  1144.         {
  1145.           lhs = symval;
  1146.     
  1147.           t = lex();
  1148.           if (t != COLON)
  1149.         fatal("ill-formed rule");
  1150.         }
  1151.  
  1152.       if (nrules == 0)
  1153.         {
  1154.           if (t == BAR)
  1155.         fatal("grammar starts with vertical bar");
  1156.  
  1157.           if (!start_flag)
  1158.         startval = lhs;
  1159.         }
  1160.  
  1161.       /* start a new rule and record its lhs.  */
  1162.  
  1163.       nrules++;
  1164.       nitems++;
  1165.  
  1166.       p = NEW(symbol_list);
  1167.       p->sym = lhs;
  1168.  
  1169.       crule1 = p1;
  1170.       if (p1)
  1171.         p1->next = p;
  1172.       else
  1173.         grammar = p;
  1174.  
  1175.       p1 = p;
  1176.       crule = p;
  1177.  
  1178.       /* mark the rule's lhs as a nonterminal if not already so.  */
  1179.  
  1180.       if (lhs->class == SUNKNOWN)
  1181.         {
  1182.           lhs->class = SNTERM;
  1183.           lhs->value = nvars;
  1184.           nvars++;
  1185.         }
  1186.       else if (lhs->class == STOKEN)
  1187.         fatals("rule given for %s, which is a token", lhs->tag);
  1188.  
  1189.       /* read the rhs of the rule.  */
  1190.  
  1191.       for (;;)
  1192.         {
  1193.           t = lex();
  1194.  
  1195.           if (! (t == IDENTIFIER || t == LEFT_CURLY)) break;
  1196.  
  1197.           /* if next token is an identifier, see if a colon follows it.
  1198.          If one does, exit this rule now.  */
  1199.           if (t == IDENTIFIER)
  1200.         {
  1201.           register bucket *ssave;
  1202.           register int t1;
  1203.  
  1204.           ssave = symval;
  1205.           t1 = lex();
  1206.           unlex(t1);
  1207.           symval = ssave;
  1208.           if (t1 == COLON) break;
  1209.  
  1210.           /* not followed by colon => process as part of this rule's rhs.  */
  1211.           if (actionflag)
  1212.             {
  1213.               bucket *sdummy;
  1214.  
  1215.               /* if this symbol was preceded by an action, */
  1216.               /* make a dummy nonterminal to replace that action in this rule */
  1217.               /* and make another rule to associate the action to the dummy.  */
  1218.               /* Since the action was written out with this rule's number, */
  1219.               /* we must write give the new rule this number */
  1220.               /* by inserting the new rule before it.  */
  1221.  
  1222.               /* make a dummy nonterminal, a gensym.  */
  1223.               sdummy = gensym();
  1224.  
  1225.               /* make a new rule, whose body is empty, before the current one.  */
  1226.               /* so that the action just read can belong to it.  */
  1227.               nrules++;
  1228.               nitems++;
  1229.               p = NEW(symbol_list);
  1230.               if (crule1)
  1231.             crule1->next = p;
  1232.               else grammar = p;
  1233.               p->sym = sdummy;
  1234.               crule1 = NEW(symbol_list);
  1235.               p->next = crule1;
  1236.               crule1->next = crule;
  1237.               
  1238.               /* insert the dummy generated by that rule into this rule.  */
  1239.               nitems++;
  1240.               p = NEW(symbol_list);
  1241.               p->sym = sdummy;
  1242.               p1->next = p;
  1243.               p1 = p;
  1244.  
  1245.               actionflag = 0;
  1246.             }
  1247.           nitems++;
  1248.           p = NEW(symbol_list);
  1249.           p->sym = symval;
  1250.           p1->next = p;
  1251.           p1 = p;
  1252.         }
  1253.           else /* handle an action.  */
  1254.         {
  1255.           copy_action(crule, rulelength);
  1256.           actionflag = 1;
  1257.         }
  1258.           rulelength++;
  1259.         }
  1260.  
  1261.       /* Put an empty link in the list to mark the end of this rule  */
  1262.       p = NEW(symbol_list);
  1263.       p1->next = p;
  1264.       p1 = p;
  1265.  
  1266.       if (t == PREC)
  1267.         {
  1268.           t = lex();
  1269.           crule->ruleprec = symval;
  1270.           t = lex();
  1271.         }
  1272.       if (t == GUARD)
  1273.         {
  1274.           if (! semantic_parser)
  1275.         fatal("%guard present but %semantic_parser not specified");
  1276.  
  1277.           copy_guard(crule, rulelength);
  1278.           t = lex();
  1279.         }
  1280.       else if (t == LEFT_CURLY)
  1281.         {
  1282.           if (actionflag) fatal("two actions at end of one rule");
  1283.           copy_action(crule, rulelength);
  1284.           t = lex();
  1285.         }
  1286.       if (t == SEMICOLON)
  1287.         t = lex();
  1288.     }
  1289.       /* these things can appear as alternatives to rules.  */
  1290.       else if (t == TOKEN)
  1291.     {
  1292.       parse_token_decl(STOKEN, SNTERM);
  1293.       t = lex();
  1294.     }
  1295.       else if (t == NTERM)
  1296.     {
  1297.       parse_token_decl(SNTERM, STOKEN);
  1298.       t = lex();
  1299.     }
  1300.       else if (t == TYPE)
  1301.     {
  1302.       t = get_type();
  1303.     }
  1304.       else if (t == UNION)
  1305.     {
  1306.       parse_union_decl();
  1307.       t = lex();
  1308.     }
  1309.       else if (t == START)
  1310.     {
  1311.       parse_start_decl();
  1312.       t = lex();
  1313.     }
  1314.       else
  1315.     fatal("illegal input");
  1316.     }
  1317.  
  1318.   if (nrules == 0)
  1319.     fatal("no input grammar");
  1320.  
  1321.   if (typed == 0)
  1322.     fprintf(fattrs1, "typedef int YYSTYPE;\n");
  1323.  
  1324.   ntokens = nsyms - nvars;
  1325. }
  1326.  
  1327.  
  1328.  
  1329. /* read in a %type declaration and record its information for get_type_name to access */
  1330.  
  1331. int
  1332. get_type()
  1333. {
  1334.   register int k;
  1335.   register int t;
  1336.   register char *name;
  1337.  
  1338.   extern char token_buffer[];
  1339.  
  1340.   t = lex();
  1341.  
  1342.   if (t != TYPENAME)
  1343.     fatal("ill-formed %type declaration");
  1344.  
  1345.   k = strlen(token_buffer);
  1346.   name = NEW2(k + 1, char);
  1347.   strcpy(name, token_buffer);
  1348.  
  1349.   for (;;)
  1350.     {
  1351.       t = lex();
  1352.  
  1353.       switch (t)
  1354.     {
  1355.     case SEMICOLON:
  1356.       return (lex());
  1357.  
  1358.     case COMMA:
  1359.       break;
  1360.  
  1361.     case IDENTIFIER:
  1362.       if (symval->type_name == NULL)
  1363.         symval->type_name = name;
  1364.       else
  1365.         fatals("type redeclaration for %s", symval->tag);
  1366.  
  1367.       break;
  1368.  
  1369.     default:
  1370.       return (t);
  1371.     }
  1372.     }
  1373. }
  1374.  
  1375.  
  1376.  
  1377. /* assign symbol numbers, and write definition of token names into fdefines.
  1378. Set up vectors tags and sprec of names and precedences of symbols.  */
  1379.  
  1380. packsymbols()
  1381. {
  1382.   register bucket *bp;
  1383.   register int tokno = 1;
  1384.   register int i;
  1385.   register int last_user_token_number;
  1386.  
  1387.   int lossage = 0;
  1388.  
  1389.   tags = NEW2(nsyms + 1, char *);
  1390.   tags[0] = "$";
  1391.  
  1392.   sprec = NEW2(nsyms, short);
  1393.   sassoc = NEW2(nsyms, short);
  1394.  
  1395.   max_user_token_number = 255;
  1396.   last_user_token_number = 255;
  1397.  
  1398.   for (bp = firstsymbol; bp; bp = bp->next)
  1399.     {
  1400.       if (bp->class == SUNKNOWN)
  1401.     {
  1402.       fprintf(stderr, "symbol %s used, not defined as token, and no rules for it\n",
  1403.               bp->tag);
  1404.       lossage = 1;
  1405.       bp->class = SNTERM;
  1406.     }
  1407.  
  1408.       if (bp->class == SNTERM)
  1409.     {
  1410.       bp->value += ntokens;
  1411.     }
  1412.       else
  1413.     {
  1414.       if (translations && !(bp->user_token_number))
  1415.         bp->user_token_number = ++last_user_token_number;
  1416.       if (bp->user_token_number > max_user_token_number)
  1417.         max_user_token_number = bp->user_token_number;
  1418.       bp->value = tokno++;
  1419.     }
  1420.  
  1421.       tags[bp->value] = bp->tag;
  1422.       sprec[bp->value] = bp->prec;
  1423.       sassoc[bp->value] = bp->assoc;
  1424.  
  1425.     }
  1426.  
  1427.   if (translations)
  1428.     {
  1429.       register int i;
  1430.  
  1431.       token_translations = NEW2(max_user_token_number+1, short);
  1432.  
  1433.       /* initialize all entries for literal tokens to 2,
  1434.      the internal token number for $illegal., which represents all illegal inputs.  */
  1435.       for (i = 0; i <= max_user_token_number; i++)
  1436.         token_translations[i] = 2;      
  1437.     }
  1438.  
  1439.   for (bp = firstsymbol; bp; bp = bp->next)
  1440.     {
  1441.       if (bp->value >= ntokens) continue;
  1442.       if (translations)
  1443.     {
  1444.       char string[50+MAXTOKEN+MAXTOKEN];
  1445.  
  1446.       if (token_translations[bp->user_token_number] != 2)
  1447.         {
  1448.           sprintf(string, "tokens %s and %s both assigned number %d",
  1449.                   tags[token_translations[bp->user_token_number]],
  1450.                   bp->tag,
  1451.                   bp->user_token_number);
  1452.           fatal(string);
  1453.         }
  1454.       token_translations[bp->user_token_number] = bp->value;
  1455.     }
  1456.     }
  1457.  
  1458.   error_token_number = errtoken->value;
  1459.  
  1460.   output_token_defines(ftable);
  1461.  
  1462.   if (startval->class == SUNKNOWN)
  1463.     fatals("the start symbol %s is undefined", startval->tag);
  1464.   else if (startval->class == STOKEN)
  1465.     fatals("the start symbol %s is a token", startval->tag);
  1466.  
  1467.   start_symbol = startval->value;
  1468.  
  1469.   if (definesflag)
  1470.     {
  1471.       output_token_defines(fdefines);
  1472.  
  1473.       if (semantic_parser)
  1474.     for (i = ntokens; i < nsyms; i++)
  1475.       {
  1476.         /* don't make these for dummy nonterminals made by gensym.  */
  1477.         if (*tags[i] != '@')
  1478.           fprintf(fdefines, "#define\tNT%s\t%d\n", tags[i], i);
  1479.       }
  1480.  
  1481.       fclose(fdefines);
  1482.       fdefines = NULL;
  1483.     }
  1484. }
  1485.       
  1486.  
  1487.  
  1488. output_token_defines(file)
  1489. FILE *file;
  1490. {
  1491.   bucket *bp;
  1492.  
  1493.   for (bp = firstsymbol; bp; bp = bp->next)
  1494.     {
  1495.       if (bp->value >= ntokens) continue;
  1496.  
  1497.       /* For named tokens, but not literal ones, define the name.  */
  1498.       /* The value is the user token number.  */
  1499.  
  1500.       if ('\'' != *tags[bp->value] && bp != errtoken)
  1501.     {
  1502.       register char *cp = tags[bp->value];
  1503.       register char c;
  1504.  
  1505.       /* Don't #define nonliteral tokens whose names contain periods.  */
  1506.  
  1507.       while ((c = *cp++) && c != '.');
  1508.       if (!c)
  1509.         {
  1510.               fprintf(file, "#define\t%s\t%d\n", tags[bp->value],
  1511.                 (translations ? bp->user_token_number : bp->value));
  1512.           if (semantic_parser)
  1513.                 fprintf(file, "#define\tT%s\t%d\n", tags[bp->value],
  1514.                   bp->value);
  1515.         }
  1516.     }
  1517.     }
  1518.  
  1519.   putc('\n', file);
  1520. }
  1521.  
  1522.  
  1523.  
  1524. /* convert the rules into the representation using rrhs, rlhs and ritems.  */
  1525.  
  1526. packgram()
  1527. {
  1528.   register int itemno;
  1529.   register int ruleno;
  1530.   register symbol_list *p;
  1531.  
  1532.   bucket *ruleprec;
  1533.  
  1534.   ritem = NEW2(nitems + 1, short);
  1535.   rlhs = NEW2(nrules, short) - 1;
  1536.   rrhs = NEW2(nrules, short) - 1;
  1537.   rprec = NEW2(nrules, short) - 1;
  1538.   rassoc = NEW2(nrules, short) - 1;
  1539.  
  1540.   itemno = 0;
  1541.   ruleno = 1;
  1542.  
  1543.   p = grammar;
  1544.   while (p)
  1545.     {
  1546.       rlhs[ruleno] = p->sym->value;
  1547.       rrhs[ruleno] = itemno;
  1548.       ruleprec = p->ruleprec;
  1549.  
  1550.       p = p->next;
  1551.       while (p && p->sym)
  1552.     {
  1553.       ritem[itemno++] = p->sym->value;
  1554.       /* a rule gets the precedence and associativity of the last token in it.  */
  1555.           if (p->sym->class == STOKEN)
  1556.         {
  1557.           rprec[ruleno] = p->sym->prec;
  1558.           rassoc[ruleno] = p->sym->assoc;
  1559.         }
  1560.       if (p) p = p->next;
  1561.     }
  1562.  
  1563.       /* if this rule has a %prec, specified symbol's precedence replaces the default */
  1564.       if (ruleprec)
  1565.     {
  1566.           rprec[ruleno] = ruleprec->prec;
  1567.           rassoc[ruleno] = ruleprec->assoc;
  1568.     }
  1569.  
  1570.       ritem[itemno++] = -ruleno;
  1571.       ruleno++;
  1572.  
  1573.       if (p) p = p->next;
  1574.     }
  1575.  
  1576.   ritem[itemno] = 0;
  1577. }
  1578.